package com.yhp.service;

import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpSession;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.system.comm.util.Snowflake;
import com.system.common.mapper.BisPostMenuMapper;
import com.system.common.mapper.BisShopPostMapper;
import com.system.common.mapper.BisShopUserMapper;
import com.system.common.mapper.SysMenuMapper;
import com.system.common.pojo.BisPostMenu;
import com.system.common.pojo.BisPostMenuExample;
import com.system.common.pojo.BisShopPost;
import com.system.common.pojo.BisShopPostExample;
import com.system.common.pojo.BisShopPostExample.Criteria;
import com.system.common.pojo.BisShopUserExample;
import com.system.common.pojo.SysMenu;
import com.system.common.pojo.SysMenuExample;
import com.system.common.pojo.SysUser;

@Service
public class BBisShopPostServiceImpl implements BBisShopPostService {
	@Autowired
	BisShopPostMapper postMapper;

	@Autowired
	SysMenuMapper sysMenuMapper;

	@Autowired
	BisPostMenuMapper bisPostMenuMapper;
	
	@Autowired
	private BisShopUserMapper BisShopUserMapper;

	/**
	 * 综合查询
	 */
	@Override
	public List<BisShopPost> selectByExample(Map<String, Object> map) {
		String postCode = (String) map.get("postCode");
		String postName = (String) map.get("postName");
		BisShopPostExample example = new BisShopPostExample();
		Criteria criteria = example.createCriteria();
		if (StringUtils.isNotBlank(postCode)) {
			criteria.andPostCodeLike("%" + postCode + "%");
		}
		if (StringUtils.isNotBlank(postName)) {
			criteria.andPostNameLike("%" + postName + "%");
		}
		return postMapper.selectByExample(example);
	}

	/**
	 * 添加
	 */
	@Override
	public int insert(BisShopPost record, String menuValue,HttpSession session) {
		SysUser user=(SysUser)session.getAttribute("user");
		Snowflake sf = new Snowflake(0, 0);
		Long postId = sf.nextId();
		record.setPostId(postId + "");
		record.setCreateTime(new Date());
		record.setCreateUserId(user.getUserId());
		postMapper.insert(record);

		String[] menuValues = menuValue.split(",");
		if (menuValues.length > 0) {
			for (String postMenu : menuValues) {
				BisPostMenu bisPostMenu = new BisPostMenu();
				bisPostMenu.setMenuId(postMenu);
				bisPostMenu.setPostId(postId + "");
				bisPostMenu.setCreateTime(new Date());
				bisPostMenu.setCreateUserId(user.getUserId());
				bisPostMenuMapper.insert(bisPostMenu);
			}
		}
		return 0;
	}

	/**
	 * 修改
	 */
	@Override
	public int updateByPrimaryKey(BisShopPost record, String menuValue,HttpSession session) {
		SysUser user=(SysUser)session.getAttribute("user");
		BisShopPost bisShopPost = postMapper.selectByPrimaryKey(record.getPostId());
		record.setCreateTime(bisShopPost.getCreateTime());
		record.setModifyTime(new Date());
		record.setCreateUserId(bisShopPost.getCreateUserId());
		record.setModifyUserId(user.getUserId());
		postMapper.updateByPrimaryKey(record);
		// 修改成功以后，删除原来的桥表信息，重新进行添加
		BisPostMenuExample example = new BisPostMenuExample();
		com.system.common.pojo.BisPostMenuExample.Criteria criteria = example.createCriteria();
		criteria.andPostIdEqualTo(bisShopPost.getPostId());
		bisPostMenuMapper.deleteByExample(example);
		String[] menusValue = menuValue.split(",");
		// 重新进行添加
		if (StringUtils.isNotBlank(menuValue)) {
			for (String meunId : menusValue) {
				BisPostMenu bisPostMenu = new BisPostMenu();
				bisPostMenu.setCreateTime(new Date());
				bisPostMenu.setCreateUserId(user.getUserId());
				bisPostMenu.setMenuId(meunId);
				bisPostMenu.setPostId(bisShopPost.getPostId());
				bisPostMenuMapper.insert(bisPostMenu);
			}
		}
		return 0;
	}

	/**
	 * 删除
	 */
	@Override
	public int deleteByPrimaryKey(String postId,String postCode) {
		//删除职位信息表
				BisShopPostExample example = new BisShopPostExample();
				Criteria criteria = example.createCriteria();
				if (StringUtils.isNotBlank(postId)) {
					criteria.andPostIdEqualTo(postId);
				}
				postMapper.deleteByExample(example);
				//删除当前职位下面的员工
				BisShopUserExample shopUserExample=new BisShopUserExample();
				com.system.common.pojo.BisShopUserExample.Criteria shopUserCrteria=shopUserExample.createCriteria();
				if (StringUtils.isNotBlank(postCode)) {
					shopUserCrteria.andPostCodeEqualTo(postCode);
				}
				BisShopUserMapper.deleteByExample(shopUserExample);
				//删除职位和菜单的桥表信息
				BisPostMenuExample menuExample = new BisPostMenuExample();
				com.system.common.pojo.BisPostMenuExample.Criteria postCrteria = menuExample.createCriteria();
				if (StringUtils.isNotBlank(postId)) {
					postCrteria.andPostIdEqualTo(postId);
				}
				return bisPostMenuMapper.deleteByExample(menuExample);
	}

	@Override
	public List<BisShopPost> selectByExample() {
		BisShopPostExample example = new BisShopPostExample();
		return postMapper.selectByExample(example);
	}

	/**
	 * ztree赋值
	 */
	@Override
	public List<SysMenu> menusCheck(String postId) {
		SysMenuExample example = new SysMenuExample();
		com.system.common.pojo.SysMenuExample.Criteria criteria = example.createCriteria();
		criteria.andMenuTypeEqualTo("2");
		List<SysMenu> menus = sysMenuMapper.selectByExample(example);
		BisPostMenuExample bisPostMenuExample = new BisPostMenuExample();
		com.system.common.pojo.BisPostMenuExample.Criteria postCrteria = bisPostMenuExample.createCriteria();
		if (StringUtils.isNotBlank(postId)) {
			postCrteria.andPostIdEqualTo(postId);
		}
		List<BisPostMenu> postMenus = bisPostMenuMapper.selectByExample(bisPostMenuExample);
		for (SysMenu sysMenu : menus) {
			for (BisPostMenu bisPostMenu : postMenus) {
				if (sysMenu.getMenuId().equals(bisPostMenu.getMenuId())) {
					sysMenu.setChecked(true);
				}
			}
		}
		return menus;
	}

	@Override
	public List<SysMenu> queryMenus() {
		SysMenuExample example = new SysMenuExample();
		com.system.common.pojo.SysMenuExample.Criteria criteria = example.createCriteria();
		criteria.andMenuTypeEqualTo("2");
		List<SysMenu> menus = sysMenuMapper.selectByExample(example);
		return menus;
	}

	@Override
	public String checkpoatCode(String postCode) {
		BisShopPostExample example = new BisShopPostExample();
		Criteria criteria = example.createCriteria();
		if (StringUtils.isNotBlank(postCode)) {
			criteria.andPostCodeEqualTo(postCode);
		}
		List<BisShopPost> bisShopPosts = postMapper.selectByExample(example);
			if (bisShopPosts.size()>0) {
				return "职位编码已经存在！！！";
			}
		return "";
	}
}
